package com.example.binpacking.controller;

import com.example.binpacking.model.Result;
import com.example.binpacking.model.dto.IndividualDto;
import com.example.binpacking.service.IndividualService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/pelicanpsyc")
public class IndividualController {

    @Autowired
    private IndividualService individualService;
    @ApiOperation("根据action执行对三种用户表更改和查询")
    @PostMapping("/api/individual")
    public Result handleIndividualRequest(@RequestBody IndividualDto request) {
        if ("edit".equals(request.getAction())) {
            boolean success = individualService.editUser(request);
            List<Map<String, Object>> data = individualService.getUserDetails(request);
            if (success) {
                return Result.success(200, "y", data);
            } else {
                return Result.fail(200, "n", Collections.emptyList());
            }
        } else if ("see".equals(request.getAction())) {
            List<Map<String, Object>> data = individualService.getUserDetails(request);
            return Result.success(200, "y", data);
        } else {
            return Result.fail(200, "n", Collections.emptyList());
        }
    }
}
